#!encoding:utf-8
from lxml import etree
import os
import urllib
import codecs


import sys

# make sure the string pass to lxml must be unicode
page=unicode(urllib.urlopen(sys.argv[1]).read(),'utf-8')
#print page
selector=etree.HTML(page)
selector=selector.xpath(sys.argv[2])
if sys.argv[2].endswith('text()') or sys.argv[3]=='a':
    for x in selector:
         print x
         f=codecs.open('FILE/1.txt','w+','utf-8')
         f.write('\n'.join(selector))
else:
    for xx in [x.xpath('string(.)') for x in selector]:
        print xx
	f=codecs.open('FILE/1.txt','w+','utf-8')
	f.write(''.join([x.xpath('string(.)') for x in selector]))
